﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solidi
{
	sealed class ContextualDependencyLifetime : IDependencyLifetime
	{
		private object _graphId;
		private IDependencyLifetime _lifetime;

		public ContextualDependencyLifetime(object graphId, IDependencyLifetime lifetime)
		{
			_graphId = graphId;
			_lifetime = lifetime;
		}

		public bool TryAcquire(object graphId, IDependencyContainer container, IDependency dependency, out object instance)
		{
			if (_graphId != graphId)
			{
				throw new Exception("Called from root");
			}

			return _lifetime.TryAcquire(graphId, container, dependency, out instance);
		}

		public bool TryAcquire(IDependencyContainer container, IDependency dependency, out object instance)
		{
			return _lifetime.TryAcquire(_graphId, container, dependency, out instance);
		}

		public bool TryDiscard(IDependencyContainer container, object instance)
		{
			return _lifetime.TryDiscard(container, instance);
		}

		public void DiscardAll(IDependencyContainer container, bool disposing)
		{
			_lifetime.TryDiscard(container, disposing);
		}
	}
}
